<?php

namespace admin\controller;

use admin\model\BlogModel;
use admin\model\upload;

class BlogController extends Controller{

    private $blog;

    public function __construct(){
        $this->blog = new BlogModel();
    }

     //查找指定id的博客并调用viwe显示
     public function getBlog($params){
        $this->setData($this->blog->getBlog($params));
        return $this->view('info');
    }
    //查询数据库中的所有博客信息并调用viwe显示
    public function getBlogAll(){
        return $this->view('manageBlog');
    }
    /**
     * 修改博客信息页面
     */
    public function editBlogInfo($params){
        $this->setData('blog',$this->blog->getBlogInfo($params));
        return $this->view('editBlog');
    }
    //查询所有的信息以 JSON 形式返回
    public function getBlogsJson($params){
        $data = $this->blog->getBlogAll($params);
        $count_data = $this->blog->getBlogCount(array('title'=>$params['title']));
        $data_json = [];
        $data_json['code'] = 0;
        $data_json['msg'] = '';
        $data_json['count'] = $count_data['count'];
        $data_json['data'] = $data;
        return json_encode($data_json);
    }
    //模糊查询博客信息并调用viwe显示
    public function queryBlog($params){
        $this->setData('blogs',$this->blog->queryBlog($params['key']));
        return $this->view('index');
    }

    //发表博客，把从前台获取的数据存储到数据库中
    public function publishBlog($params){
        $data = $this->blog->publishBlog($this->filterHTML(($params)));  //操作数据库并返回状态嘛
        return json_encode($data);      //返回给前端 ajax 
    }

    //设置博客为私密
    public function setBlogPrivate($params){
        $data = $this->blog->privateBlog($params);  //操作数据库并返回状态嘛
        return json_encode($data);      //返回给前端 ajax 
    }
    /**
     * 设置博客部分信息
     */
    public function editBlog($params){
        return $this->blog->updateBlog($params);
    }
    /**
     * 修改博客全部信息
     */
    public function editBlogAll($params){
        $data = $this->blog->updateBlogAll($params);
        return json_encode($data);
    }
    /**
     * 过滤特殊字符
     */
    public function filterHTML($params){
        $params['title'] = htmlspecialchars($params['title']);          //过滤特殊字符
        $params['outline'] = htmlspecialchars($params['outline']);      
        $params['title'] = str_replace(chr(13),'<br>',$params['title']);        //将换行取消转换
        $params['outline'] = str_replace(chr(13),'<br>',$params['outline']);
        $params['title'] = str_replace(chr(32),'&nbsp;',$params['title']);      //将空格取消转换
        $params['outline'] = str_replace(chr(32),'&nbsp;',$params['outline']);
        return $params;
    }
    /**
     * 批量删除博客
     */
    public function delBlog($params){
        $data = $this->blog->delBlog($params);
        return json_encode($data);
    }

}